/*
能发送异步ajax请求的函数模块
封装axios库
函数的返回值是promise对象
1. 优化1: 统一处理请求异常?
    在外层包一个自己创建的promise对象
    在请求出错时, 不reject(error), 而是显示错误提示
2. 优化2: 异步得到不是reponse, 而是response.data
   在请求成功resolve时: resolve(response.data)
 */

import axios from "axios";
import { Message, Loading } from "element-ui";

let loading;

function startLoading() {
  loading = Loading.service({
    lock: true,
    text: "拼命加载..."
  });
}
function endLoading() {
  loading.close();
}
// Add a request interceptor
axios.interceptors.request.use(
  function(config) {
    // Do something before request is sent
    startLoading();
    // if (localStorage.eleToken) {
    //   config.headers.Authorization = localStorage.eleToken;
    // }
    return config;
  },
  function(error) {
    // Do something with request error
    return Promise.reject(error);
  }
);

// Add a response interceptor
axios.interceptors.response.use(
  function(response) {
    // Do something with response data
    endLoading();
    return response;
  },
  function(error) {
    // Do something with response error
    endLoading();
    Message.error(error.response.data);
    const { status } = error.response;
    // if (status == 401) {
    //   Message.error("token失效，请重新登录");
    //   localStorage.removeItem("eleToken");
    //   router.push("/login");
    // }
    return Promise.reject(error);
  }
);

export default function ajax(url, data = {}, type = "GET") {
  return new Promise((resolve, reject) => {
    let promise;
    // 1. 执行异步ajax请求
    if (type === "GET") {
      // 发GET请求
      promise = axios.get(url, {
        // 配置对象
        params: data // 指定请求参数
      });
    } else {
      // 发POST请求
      promise = axios.post(url, data);
    }
    // 2. 如果成功了, 调用resolve(value)
    promise
      .then(response => {
        resolve(response.data);
        // 3. 如果失败了, 不调用reject(reason), 而是提示异常信息
      })
      .catch(error => {
        // reject(error)
        Message.error("请求出错了: " + error.message);
      });
  });
}
